package com.boge.modules.house.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.boge.common.utils.ShiroUtils;
import com.boge.modules.house.entity.TAreaEntity;
import com.boge.modules.house.entity.TProvinceEntity;
import com.boge.modules.house.entity.TTypeEntity;
import com.boge.modules.house.service.TAreaService;
import com.boge.modules.house.service.TProvinceService;
import com.boge.modules.house.service.TTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boge.modules.house.entity.THouseEntity;
import com.boge.modules.house.service.THouseService;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.R;



/**
 * 
 *
 * @author boge
 * @email dengpbs@163.com
 * @date 2024-12-04 22:12:55
 */
@RestController
@RequestMapping("house/thouse")
public class THouseController {
    @Autowired
    private THouseService tHouseService;

    @Autowired
    private TTypeService tTypeService;

    @Autowired
    private TProvinceService provinceService;

    @Autowired
    private TAreaService tAreaService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("house:thouse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tHouseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("house:thouse:info")
    public R info(@PathVariable("id") Integer id){
		THouseEntity tHouse = tHouseService.getById(id);
        // 查询出所有的 身份信息
        List<TProvinceEntity> provinces = provinceService.list();
        // 查询出所有的 类别数据
        if(id == null || id == 0){
            List<TTypeEntity> list = tTypeService.list();
            return R.ok().put("tHouse", tHouse).put("types",list).put("provinces",provinces)
                    ;
        }

        return R.ok()
                .put("tHouse", tHouse)
                .put("provinces",provinces)
                ;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("house:thouse:save")
    public R save(@RequestBody THouseEntity tHouse){
        // 设置初始值
        tHouse.setStatus(0);
        if(tHouse.getAreaId() != null && !"".equals(tHouse.getAreaId())){
            //根据 地区id 找到对应的 地区code
            TAreaEntity areaEntity = tAreaService.getById(tHouse.getAreaId());
            tHouse.setAreaCode(areaEntity.getCode());
        }

        // 获取当前登录用户
        Long userId = ShiroUtils.getUserId();
        tHouse.setUserId(userId.intValue());
		tHouseService.save(tHouse);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("house:thouse:update")
    public R update(@RequestBody THouseEntity tHouse){
		tHouseService.updateById(tHouse);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("house:thouse:delete")
    public R delete(@RequestBody Integer[] ids){
		tHouseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
